package com.orgcent.tuangou.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 本地数据库Helper*/
public class DBHelper {
    public static final String DB_NAME = "tuangou.db";
    public static final String DB_TABLE = "CITYS";
    public static final int DB_VERSION = 1;
    
    private static final String[] COLS = new String[] { "_id","name", "value","type"};

    private SQLiteDatabase db;
    private final DBOpenHelper dbOpenHelper;

   

    private static class DBOpenHelper extends SQLiteOpenHelper {

        private static final String DB_CREATE = "CREATE TABLE "
            + DBHelper.DB_TABLE
            + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, value TEXT , type TEXT);";

        public DBOpenHelper(final Context context) {
            super(context, DBHelper.DB_NAME, null, DBHelper.DB_VERSION);
        }

        @Override
        public void onCreate(final SQLiteDatabase db) {
            try {
                db.execSQL(DBOpenHelper.DB_CREATE);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DBHelper.DB_TABLE);
            onCreate(db);
        }
    }

    //
    // end inner classes
    //

    public DBHelper(final Context context) {
        this.dbOpenHelper = new DBOpenHelper(context);
        establishDb();
    }

    private void establishDb() {
        if (this.db == null) {
            this.db = this.dbOpenHelper.getWritableDatabase();
        }
    }

    public void cleanup() {
        if (this.db != null) {
            this.db.close();
            this.db = null;
        }
    }

    public Cursor rawQuery(String sql,String[] selectionArgs) {
    	return this.db.rawQuery(sql, selectionArgs);
    }
    
    /**
     * 获取特定号码，最近一次来电时间
     * @param incomingNum
     * @return
     */
    public String getAddTime(String incomingNum) {
    	Cursor c = null;
    	try {
    		c = this.db.rawQuery("select add_date from " 
       			 + DBHelper.DB_TABLE + " where address=? order by _id desc", 
       			 new String[]{incomingNum});
	    	if(c.moveToFirst()) {
	    		return c.getString(c.getColumnIndex("add_date"));
	    	} else {
	    		return null;
	    	}
    	} catch (Exception e) {
    		e.printStackTrace();
    		return null;
		} finally {
	    	if(c != null) {
	    		c.close();
	    	}
	    	cleanup();
    	}
    }
    
    public long insert(String name,String value, String type) {
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("value", value);
        values.put("type", type);
        
        long id = this.db.insert(DBHelper.DB_TABLE, "", values);
//        cleanup();//关闭连接
        return id;
    }

    public void delete(final long id) {
        this.db.delete(DBHelper.DB_TABLE, "_id=" + id, null);
        cleanup();//关闭连接
    }

    public int deleteAll() {
        int row = this.db.delete(DBHelper.DB_TABLE, null, null);
        cleanup();//关闭连接
        return row;
    }
}
